package systemtray;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionListener;

import kevin.kCachedFileLoader;

public class ScriptTrayManager {
	private TrayIcon trayIcon;
	private PopupMenu popup = new PopupMenu();

	public ScriptTrayManager() {
		super();
	}

	public ScriptTrayManager(String scriptName, double version, String imageLocation) {
		this(scriptName, version, Toolkit.getDefaultToolkit().getImage(
				new kCachedFileLoader(imageLocation).getURL()));
	}

	public ScriptTrayManager(String scriptName, double version, Image img) {
		trayIcon = new TrayIcon(img, scriptName + " v" + version);

		if (SystemTray.isSupported()) {
			if (trayIcon != null) {
				trayIcon.setToolTip(scriptName + " v" + version);
				trayIcon.setImageAutoSize(true);
				try {
					SystemTray.getSystemTray().add(trayIcon);
				} catch (AWTException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void dispose() {
		if (trayIcon != null) {
			SystemTray.getSystemTray().remove(trayIcon);
		}
	}

	public void add(MenuItem item, ActionListener al) {
		item.addActionListener(al);
		popup.add(item);
		trayIcon.setPopupMenu(popup);
	}

	public void displayMessage(String title, String msg, TrayIcon.MessageType type) {
		trayIcon.displayMessage(title, msg, type);
	}
}
